package cn.itsource.xhy.basic.service.impl;

import cn.itsource.xhy.basic.mapper.BaseMapper;
import cn.itsource.xhy.basic.query.BaseQuery;
import cn.itsource.xhy.basic.service.IBaseService;
import cn.itsource.xhy.utils.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {
    
    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    @Transactional
    public void save(T t) {
        baseMapper.save(t);
    }

    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    @Transactional
    public void del(Long id) {
        baseMapper.del(id);
    }

    @Override
    public T findById(Long id) {
        return baseMapper.findById(id);
    }

    @Override
    public List<T> findAll() {
        return baseMapper.findAll();
    }

    @Override
    public QueryResult<T> queryPage(BaseQuery query) {
        Long total = baseMapper.count(query);
        if (total==null||total == 0){
            return new QueryResult<>();
        }
        List<T> data = baseMapper.queryPage(query);
        return new QueryResult<>(data,total);

    }

    @Override
    @Transactional
    public void patchDel(Long[] ids) {
        baseMapper.patchDel(ids);
    }
}
